package TableModels;

import Database.DBLoader;
import GUI.PreferenceFrame;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

public class AttributeTableModelCuanti extends DefaultTableModel{
    
    /*
     * clase AttributesTableModel
     * 
     * Esta clase provee un modelo de tabla atributos.
     * 
     * Su vista proporciona Nombre de Atributo, Tipo y factor MAUT asociado.
     * 
     * Se le debe pasar un objeto con la base de datos, y llamar a load()
     * para que realice la carga.
     * 
     */
    
    private DBLoader database;
    private Vector<Object> rows;
    private PreferenceFrame parent;
    private Hashtable<Integer, Double> changedFactor;
    private Hashtable<Integer, Boolean> changedExcluyente;
    private Hashtable<Integer, Double> changedUmbral;

    public AttributeTableModelCuanti(DBLoader database, PreferenceFrame frame) {
        super(new Object[]{"Nombre Atributo", "Tipo", "Factor", "Excluyente", "Valor Umbral"}, 0);
        parent = frame;
        rows = new Vector<Object>();
        this.database = database;
        changedFactor = new Hashtable<Integer, Double>();
        changedExcluyente = new Hashtable<Integer, Boolean>();
        changedUmbral = new Hashtable<Integer, Double>();
    }
    
    public boolean load() throws SQLException {
        int rowCount = getRowCount();
        for (int i = 0; i < rowCount; i++) {
            removeRow(0);
        }
        if (database.isConnected()) {
            ResultSet tablaAtributo = database.executeQuery("SELECT * FROM APP.ATRIBUTO WHERE NOT TIPO='" + "NAN" + "'");
            while (tablaAtributo.next()) {
                Object[] row = new Object[5];
                row[0] = tablaAtributo.getString("NOMBRE");
                row[1] = tablaAtributo.getString("TIPO");
                row[2] = tablaAtributo.getString("FACTOR");
                row[3] = Boolean.valueOf(tablaAtributo.getBoolean("EXCLUYENTE"));
                if ((((String) row[1]).equals("INTEGER")) || (((String) row[1]).equals("REAL"))) {
                    row[4] = tablaAtributo.getDouble("UMBRAL");
                    addRow(row);
                    rows.add(row);
                }
            }
            return true;
        }
        return false;
    }
    
    @Override
     public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
    
    @Override
    public void setValueAt(Object o, int r, int c) {
        if (c == 2) {
            try {
                Double a = Double.valueOf((String) o);
                ((Object[]) rows.get(r))[c] = o;
                tableChanged(r, c);
            } catch (NumberFormatException e) {
                parent.setErrorMsg("Fila " + r + ": Error de formato");
            }
        } else {
            ((Object[]) rows.get(r))[c] = o;
            tableChanged(r, c);
        }
    }    
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        return (((Object[]) rows.get(rowIndex))[columnIndex]);
    }    
    
    @Override
    public boolean isCellEditable(int row, int column) {
        if ((column > 1)) {
            return true;
        }
        return false;
    }

    public void tableChanged(int r, int c) {
        if (c == 2) {
            changedFactor.put(r, Double.parseDouble(String.valueOf(getValueAt(r, 2))));
        } else if (c == 3) {
            changedExcluyente.put(r, Boolean.valueOf(String.valueOf(getValueAt(r, 3))));
        } else if (c == 4) {
            changedUmbral.put(r, Double.valueOf(getValueAt(r, 4).toString()));
            System.out.println(Double.valueOf(getValueAt(r, 4).toString()));
        }
    }
    
    public Hashtable<Integer, Double> getChangedRowsFactor() {
        Hashtable<Integer, Double> copia = (Hashtable<Integer, Double>) changedFactor.clone();
        changedFactor.clear();
        return copia;
    }

    public Hashtable<Integer, Boolean> getChangedRowsExcluyente() {
        Hashtable<Integer, Boolean> copia = (Hashtable<Integer, Boolean>) changedExcluyente.clone();
        changedExcluyente.clear();
        return copia;
    }

    public Hashtable<Integer, Double> getChangedRowsUmbral() {
        Hashtable<Integer, Double> copia = (Hashtable<Integer, Double>) changedUmbral.clone();
        changedUmbral.clear();
        return copia;
    }
}
